const path = require('path')
const svgoConfig = require('./config/svgo')

function resolve (dir) {
  return path.join(__dirname, '.', dir)
}

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons')).end()
      .end()
      .rule('svg-sprite-loader').test(/\.(svg)(\?.*)?$/)
      .include.add(resolve('src/assets/icons')).end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({ symbolId: 'icon-[name]' })
      .end()
      .use('svgo-loader')
      .loader('svgo-loader')
      .options(svgoConfig)
      .end()

    config
      .when(process.env.NODE_ENV === 'production', config => {
        /**
         * 此处修改导致vue.config.js 的配置项
         * productionSourceMap 失效
         * parallel 失效
         * 如需修改请修改conig/terser.js
         */
        const TerserPlugin = require('terser-webpack-plugin')
        const terserOptions = require('./config/terser')
        config.optimization.minimizer([
          new TerserPlugin(terserOptions)
        ])
      })
  }
}
